<?php
/**
 * 	Bushido
 *
 * @category   Bushido
 * @package    Bushido_Controller_Action_Secure
 * @copyright  Copyright (c) 2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: ImageController.php 1006 2009-08-11 19:26:22Z Nathan Keyes $
 */
class Bushido_Controller_Action_Image extends Bushido_Controller_Action
{

	public function viewAction()
	{
		$this->_helper->layout->disableLayout();
		$imageId = $this->_getParam('image_id');		
		$images = Bushido_Db_Table::getTable('Bushido_DbTable_Image');		
		$image = $images->find($imageId)->current();
		
		
		//Zend_Debug::dump($lastModified);exit;
		if($image && $image->findParentBushido_DbTable_Gallery()->is_public)
		{
			$lastModifiedTimeStamp = new Zend_Date($image->getLastUpdatedDate());
			$lastModifiedTimeStamp = $lastModifiedTimeStamp->getTimestamp();
			$lastModified = gmdate("D, d M Y H:i:s", $lastModifiedTimeStamp)." GMT";
			$eTag = md5($image->content);
			
			//Zend_Debug::dump($_SERVER, '$_SERVER');exit;
			$this->getResponse()
			->clearHeaders()
			->setHeader('Content-type', $image->mime_type)
			->setHeader('Content-length', $image->size)
			->setHeader('Cache-Control', 'public, max-age=31536000', true)
			->setHeader('Last-Modified', $lastModified, true)
			->setHeader('E-Tag', $eTag, true);
			if(strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModifiedTimeStamp || trim($_SERVER['HTTP_IF_NONE_MATCH']) == $eTag)
			{
				$this->getResponse()->setHttpResponseCode(304)->sendResponse();
				return;
			}
			
			
			
			//->setHeader('Content-disposition', 'attachment;')
			//->setHeader('Content-disposition', 'attachment;filename="' . $image->filename . '"')
			$this->getResponse()->setBody($image->content);
			//Zend_Debug::dump($this->getResponse()->getHeaders());exit;
			$this->getResponse()->sendResponse();
		}
		else
		{
			throw new Bushido_Db_Table_Exception_RowNotFound();
		}
	}
}